#define F_CPU 20000000UL  // 20 MHz CPU clock

#include <stdio.h>
#include <stdlib.h>
#include <avr/io.h>
#include <avr/interrupt.h>
#include <util/delay.h>

#include "serial.h"

// Global variables
Serialt serial;
uint8_t charIn; 								//USART incoming character

void commMonitor(void)
{
	// Deal with incoming serial bytes here
	if (serial_read(&serial,&charIn)) //if there's data read it
	{
		serial_write(&serial, charIn); //echo it back out for now...
		switch (charIn)
		{
			case '1':
				// LED 1 on
				PORTD |= _BV(PD6);
				break;
			case '2':
				// LED 1 off
				PORTD &= ~_BV(PD6);
				break;
			case '3':
				// LED 2 on
				PORTD |= _BV(PD7);
				break;
			case '4':
				// LED 2 off
				PORTD &= ~_BV(PD7);
				break;
			case '9':
				// LED 0 on
				PORTC |= _BV(PC0);
				break;
			case '0':
				// LED 0 off
				PORTC &= ~_BV(PC0);
				break;

			default:
				break;
		}

	}

	return;
}


int main(void) 
{
	
	// Initialization

	
	// 	Set up serial port
	serial_init(&serial);
	
	//	Set up IO pin
	DDRC  |= _BV(PC0); // set led pin to output direction led0
	PORTC &= ~_BV(PC0); // led on, pin high

	DDRD  |= _BV(PD6); // set led pin to output direction led1
	PORTD &= ~_BV(PD6); // led on, pin high
	
	DDRD  |= _BV(PD7); // set led pin to output direction led2
	PORTD &= ~_BV(PD7); // led on, pin high
		
	sei(); // Globally enable interrupts
         
    while (1)                       /* loop forever */
    {
		commMonitor();	// Monitor serial port for incoming bytes
    }
	
	
	return 0;
}
